package com.goldenautumn.prefabricate.activity.mine

import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.mine.adapter.LanguageAdapter
import com.goldenautumn.prefabricate.databinding.ActivityNoticeBinding
import com.goldenautumn.prefabricate.utils.clickIsLogin
import com.gyf.immersionbar.ImmersionBar
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import kotlinx.coroutines.isActive
import qncx.external.common.base.BaseActivity
import qncx.external.common.compat.gone
import qncx.external.common.compat.isVisible
import qncx.external.common.widget.dialog.PermissionDialog

class NoticeActivity : BaseActivity<ActivityNoticeBinding>() {

    private val mAdapter by lazy {
        LanguageAdapter()
    }

    val PERMISSIONS_NOTIFICATION  = arrayListOf(Permission.NOTIFICATION_SERVICE,Permission.POST_NOTIFICATIONS)

    override fun getLayoutId(): Int = R.layout.activity_notice

    override suspend fun main() {
        ImmersionBar.setTitleBar(this, mBinding.toolbar)
        initToolbarNav(mBinding.toolbar)

        mBinding.noticeRv.layoutManager = LinearLayoutManager(this)
        mBinding.noticeRv.adapter = mAdapter
        mAdapter.setStateViewLayout(this,R.layout.recycler_empty_view)
        var emptyTv = mAdapter.stateView?.findViewById<TextView>(R.id.prompt_content)
        emptyTv?.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(this, R.mipmap.ic_notice_empty), null, null)
        emptyTv?.text = getString(R.string.no_news_info)
        mAdapter.isStateViewEnable = true

        mBinding.noticeBody.isVisible(!XXPermissions.isGranted(this, PERMISSIONS_NOTIFICATION))

        mBinding.closeBtn.clickIsLogin {
            mBinding.noticeBody.gone()
        }

        mBinding.goBtn.setOnClickListener {
            var dialgo = PermissionDialog(resources.getStringArray(R.array.user_agreement),5) {
            }
            dialgo.setCanTouchCancel(false)
            dialgo.onShow(supportFragmentManager.beginTransaction())
        }

        while (isActive) {
            events.receive()
        }

    }

}